package com.example.carbonbackend.service.impl;

import com.example.carbonbackend.common.BaseResponse;
import com.example.carbonbackend.service.CompanyService;
import com.example.carbonbackend.service.OssUploadService;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigInteger;

import java.io.File;
import java.io.IOException;

/**
 * 将数据写入word文档
 */
@Service
public class WordWriter {
    @Autowired
    CompanyService companyService;
    public static String createEGWordDocument(String name,String year,Double e,Double eBurn
    ,Double eDesul,Double eElec) throws IOException {
        // 创建文档对象

        XWPFDocument document = new XWPFDocument();
        XWPFParagraph titleParagraph = document.createParagraph();
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(name+year+"年二氧化碳排放量报告");
        titleRun.setFontSize(16);
        titleRun.setBold(true);
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        titleParagraph.setSpacingAfter(200);
        // 创建段落对象
        XWPFParagraph paragraph = document.createParagraph();

        // 创建表格对象
        XWPFTable table = document.createTable(4, 2); // 四行两列的表格

        // 填充数据
        String[] categories = {
                "企业二氧化碳排放总量（tCO2）",
                "化石燃料燃烧排放量（tCO2）",
                "脱硫过程排放量（tCO2）",
                "净购入使用的电力排放量（tCO2）"
        };
        String[] values = {e.toString(), eBurn.toString(), eDesul.toString(), eElec.toString()};

        for (int i = 0; i < categories.length; i++) {
            XWPFTableRow row = table.getRow(i);
            row.getCell(0).setText(categories[i]);
            row.getCell(1).setText(values[i]);
        }

        // 设置表格宽度
        table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000)); // 100% 宽度


        File file = File.createTempFile("temp", ".docx");
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            document.write(outputStream);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // 读取文件内容
        InputStream inputStream = new FileInputStream(file);
        // 创建 MockMultipartFile 对象
        MultipartFile multipartFile = new MockMultipartFile(
                file.getName(),      // 文件名
                file.getName(),      // 原始文件名
                "application/octet-stream", // 内容类型
                inputStream          // 文件内容流
        );

        OssUploadService ossUploadService=new OssUploadServiceImpl();

        BaseResponse<String> baseResponse=ossUploadService.uploadFile(multipartFile);

        String url=baseResponse.getData();

        try (FileOutputStream out = new FileOutputStream("E:\\CarbonDocument1.docx")) {
            document.write(out); // 将文档写入到输出流
            System.out.println("Word 文档已成功创建！");
        } catch (IOException ex) {
            ex.printStackTrace(); // 打印异常信息
        } finally {
            // 关闭文档
            try {
                document.close(); // 关闭文档
            } catch (IOException ex) {
                ex.printStackTrace(); // 打印异常信息
            }
        }

        return url;
    }
    public static String createEUWordDocument(String name,String year,Double e,Double eTran
            ,Double eSf6) throws IOException {
        // 创建文档对象

        XWPFDocument document = new XWPFDocument();
        XWPFParagraph titleParagraph = document.createParagraph();
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(name+year+"年二氧化碳排放量报告");
        titleRun.setFontSize(16);
        titleRun.setBold(true);
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        titleParagraph.setSpacingAfter(200);
        // 创建段落对象
        XWPFParagraph paragraph = document.createParagraph();

        // 创建表格对象
        XWPFTable table = document.createTable(3, 2); // 四行两列的表格

        // 填充数据
        String[] categories = {
                "企业二氧化碳排放总量（tCO2）",
                "使用六氟化硫设备修理与退役过程产\n" +
                        "生的排放（tCO2）",
                "输配电引起的二氧化碳排放（tCO2））",
        };
        String[] values = {e.toString(), eTran.toString(), eSf6.toString()};

        for (int i = 0; i < categories.length; i++) {
            XWPFTableRow row = table.getRow(i);
            row.getCell(0).setText(categories[i]);
            row.getCell(1).setText(values[i]);
        }

        // 设置表格宽度
        table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000)); // 100% 宽度


        File file = File.createTempFile("temp", ".docx");
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            document.write(outputStream);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // 读取文件内容
        InputStream inputStream = new FileInputStream(file);
        // 创建 MockMultipartFile 对象
        MultipartFile multipartFile = new MockMultipartFile(
                file.getName(),      // 文件名
                file.getName(),      // 原始文件名
                "application/octet-stream", // 内容类型
                inputStream          // 文件内容流
        );

        OssUploadService ossUploadService=new OssUploadServiceImpl();

        BaseResponse<String> baseResponse=ossUploadService.uploadFile(multipartFile);

        String url=baseResponse.getData();

        try (FileOutputStream out = new FileOutputStream("E:\\CarbonDocument2.docx")) {
            document.write(out); // 将文档写入到输出流
            System.out.println("Word 文档已成功创建！");
        } catch (IOException ex) {
            ex.printStackTrace(); // 打印异常信息
        } finally {
            // 关闭文档
            try {
                document.close(); // 关闭文档
            } catch (IOException ex) {
                ex.printStackTrace(); // 打印异常信息
            }
        }

        return url;
    }

}